<?php

class Application_Model_Message extends Ahs_Model
{
    /**
     * @var integer
     */
    protected $_id;   
    /**
     * @var Ahs_Model_User
     */
    protected $_userto_id;
    /**
     * @var Ahs_Model_User
     */
    protected $_userfrom_id; 
    /**
     * @var timestamp
     */
    protected $_senddate;
    /**
     * @var string
     */
    protected $_subject;
    /**
     * @var string
     */
    protected $_content;
    /**
     * @var integer
     */
    protected $_parent_id;
    /**
     * @var array
     */
    protected $_isread_by       = array();
    /**
     * @var array
     */
    protected $_isdeleted_for   = array();
    /**
     * @var datetime
     */
    protected $_deleted_date;
    
    
    /**
     * @return integer
     */    
    public function getId() {
        return $this->_id;
    }
    /**
     * @param integer $id
     */
    public function setId($id) {
        $this->_id = $id;
    }
    
    /**
     * @return Ahs_Model_User
     */    
    public function getUserToId() {
        return $this->_userto_id;
    }
    /**
     * @param integer $userto
     */
    public function setUserToId($userto_id) {
        $this->_userto_id = $userto_id;
    }
    /**
     * @return Application_Model_User
     */
    public function getUserTo()
    {
        return $this->_getUserById($this->_userto_id);
    }
    
    /**
     * @return integer
     */    
    public function getUserFromId() {
        return $this->_userfrom_id;
    }
    /**
     * @param integer $userfrom_id
     */
    public function setUserFromId($userfrom_id) {
        $this->_userfrom_id = $userfrom_id;
    }
    /**
     * @return Application_Model_User
     */
    public function getUserFrom()
    {
        return $this->_getUserById($this->_userfrom_id);
    }
   
     /**
     * @return timestamp
     */
    public function getSendDate() {
        return $this->_senddate;
    }
    /**
     * @param timestamp $senddate
     */
    public function setSendDate($senddate) {
        $this->_senddate = $senddate;
    }
    
    /**
     * @return string
     */
    public function getSubject() {
        return $this->_subject;
    }
    /**
     * @param string $subject
     */
    public function setSubject($subject) {
        $this->_subject = $subject;
    }
    
    /**
     * @return string
     */
    public function getContent() {
        return $this->_content;
    }
    /**
     * @param string $content
     */
    public function setContent($content) {
        $this->_content = $content;
    }
    
    /**
     * @return integer
     */    
    public function getParentId() {
        return $this->_parent_id;
    }
    /**
     * @param integer $parent_id
     */
    public function setParentId($parent_id) {
        $this->_parent_id = $parent_id;
    }
    
    /**
     * @return array
     */
    public function getIsReadBy() {
        return $this->_isread_by;
    }
    /**
     * @param array $is_read
     */
    public function setIsReadBy($is_read) {
        if(!is_array($is_read))
        {
            if(strlen($is_read))
                $is_read = explode(",", $is_read);
            else
                $is_read = array();
        }
        
        $this->_isread_by = $is_read;
    }
    
    /**
     * @return array
     */
    public function getIsDeletedFor() {
        return $this->_isdeleted_for;
    }
    /**
     * @param array $is_deleted
     */
    public function setIsDeletedFor($is_deleted) {
        if(!is_array($is_deleted))
        {
            if(strlen($is_deleted))
                $is_deleted = explode(",", $is_deleted);
            else
                $is_deleted = array();
        }
        
        $this->_isdeleted_for = $is_deleted;
    }
    
    /**
     * @return datetime 
     */
    public function getDeletedDate()
    {
        return $this->_deleted_date;
    }
    /**
     * @param datetime $date 
     */
    public function setDeletedDate($date)
    {
        $this->_deleted_date = $date;
    }
    
    /**
     * Get the number of undread messages for this message and its children
     */
    public function getUnread()
    {
        if($this->_id != null)
        {
            $mapper = new Application_Model_MessageMapper();
            return $mapper->fetchUnreadMessagesCountByParentId($this->_id);
        }
        
        return 0;
    }
    
    /**
     * Fetch the User Object for the given UserId
     * @param integer $user_id
     * @return Application_Model_User 
     */
    private function _getUserById($id)
    {
        if($id != null)
        {
            $mapper = new Application_Model_UserMapper();
            return $mapper->fetchById($id);
        }
        
        return null;
    }
}



